//
//  CPWelcomeViewController.swift
//  weibo
//
//  Created by zcp on 16/5/13.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit
//import SnapKit
import SDWebImage
class CPWelcomeViewController: UIViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置图像
        setUserIcon()
        //在修改用户图像数据时,回调更新这个图像
        CPUserAccountViewModel.sharedAccountViewModel.loadUserInfro { () -> () in
            self.setUserIcon()
        }
        //添加控件
        view.addSubview(bgImageView)
        view.addSubview(userView)
        view.addSubview(label)
        //添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        userView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(userView)
            make.top.equalTo(userView.snp_bottom).offset(16)
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        addAnimation()
    }
    //设置用户图像
    private func setUserIcon(){
    
        if let result = CPUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large{
            // 获取到了用户数据,设置头像
            let url = NSURL(string: result)!
            self.userView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
       
    }

    //添加动画
    private func addAnimation(){
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.userView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view.snp_top).offset(160)
                
            })
            self.view.layoutIfNeeded()
            }, completion: { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        //动画完成,跳转到首页控制器
                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
                        
                        appDelegate.switchRootViewController(CPMainTabbarViewController())
                })
                
        })
    }

//MARK - 懒加载控件
    
    lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    lazy var userView: UIImageView = {
        
        let userView = UIImageView(image: UIImage(named: "avatar_default_big"))
        userView.layer.cornerRadius = 42.5
        userView.layer.masksToBounds = true
        
        return userView
        
        }()
    lazy var label: UILabel = {
    
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()

}
